<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{ active: visible }">
      <span class="placeholder">{{ fullLocation }}</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="visible">
      <div v-if="loading" class="loading"></div>
      <template v-else>
        <span
          class="ellipsis"
          v-for="item in curList"
          :key="item.code"
          @click="changeItem(item)"
          >{{ item.name }}</span
        >
      </template>
    </div>
  </div>
</template>

<script>
import { onClickOutside } from '@vueuse/core'
import { ref, reactive, computed } from 'vue'
import axios from 'axios'
const getCityData = () => {
  const url = 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
  return axios({ url })
}
export default {
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    const visible = ref(false)
    const cityData = ref([])
    const loading = ref(false)
    // 显示城市组件
    const open = () => {
      visible.value = true
      // 检查在window中是否有数据
      if (window.cityData) {
        // 有数据就从window拿取
        cityData.value = window.cityData
      } else {
        // 没有数据就获取数据
        // 获取城市数据、
        // 加载中
        loading.value = true
        getCityData().then(res => {
          cityData.value = res.data
          // 把数据存到window中
          window.cityData = res.data
          // 加载完成
          loading.value = false
        })
      }
    }

    // 隐藏城市组件
    const close = () => {
      visible.value = false
      // 清空选择的痕迹
      for (const key in selectResult) {
        selectResult[key] = ''
      }
    }

    // 切换显示和隐藏
    const toggle = () => {
      visible.value ? close() : open()
    }

    // 点击其他位置隐藏
    const target = ref(null)
    onClickOutside(target, () => {
      close()
    })

    // 定义保存用户点击的数据
    const selectResult = reactive({
      provinceCode: '', // 省
      provinceName: '', // 省的名字
      cityCode: '', // 市
      cityName: '', // 市的名字
      countyCode: '', // 区
      countyName: '', // 区的名字
      fullLocation: '' // 组合
    })

    const curList = computed(() => {
      // 省份
      let currList = cityData.value
      // 城市
      if (selectResult.provinceCode) {
        currList = currList.find(p => p.code === selectResult.provinceCode).areaList
      }
      // 地区
      if (selectResult.cityCode) {
        currList = currList.find(c => c.code === selectResult.cityCode).areaList
      }
      return currList
    })

    // 当前点击的城市
    const changeItem = (item) => {
      if (item.level === 0) {
        // 点击的是省(保存用户每次点击的选择)
        selectResult.provinceCode = item.code
        selectResult.provinceName = item.name
      }
      if (item.level === 1) {
        // 点击的是市(保存用户每次点击的选择)
        selectResult.cityCode = item.code
        selectResult.cityName = item.name
      }
      if (item.level === 2) {
        // 点击的是区(保存用户每次点击的选择)
        selectResult.countyCode = item.code
        selectResult.countyName = item.name
        selectResult.fullLocation = selectResult.provinceName + selectResult.cityName + selectResult.countyName
        // 通知父组件当前用户的选择
        emit('adress-change', selectResult)
        // 关闭弹层,通知父组件当前用户的选择
        close()
      }
    }
    return { visible, toggle, target, changeItem, curList, loading }
  }
}
</script>
<style scoped lang="less">
.xtx-city .select .placeholder {
  color: #000 !important;
}
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
.loading {
  height: 290px;
  width: 100%;
  background: url("~@/assets/images/loading.gif") no-repeat center;
}
</style>
